package team.wuyou.service.imp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.wuyou.bean.AccountAndPermission;
import team.wuyou.util.encryption.MD5Utils;
import team.wuyou.bean.Result;
import team.wuyou.bean.User;
import team.wuyou.dao.UserMapper;
import team.wuyou.service.UserService;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImp implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public Result selectUser(Map map, HttpSession session) {
        System.out.println("进入service，接收到的值为："+map);
        Result result = new Result();
        List<Map> users = userMapper.selectUser(map);

        System.out.println("查询结果为:"+"\n"+users);
        //如果查询到有值则登录成功
        if (users.size()==1){
            result.setCode("1");
            result.setMsg("登陆成功");
            result.setData(users);
            session.setAttribute("username",map.get("username"));
        }else {
            result.setCode("0");
            result.setMsg("登陆失败");
        }
        return result;
    }

    @Override
    public Result insert(Map map,Map permissionmap) {
        Result result = new Result();
        AccountAndPermission accountAndPermission = new AccountAndPermission();
        System.out.println("进入service，接收到的值为："+map);

        //判断是否为空,加密，为空则不加密，不为空则加密
        if (map.get("password").equals("")||map.get("password") == null) {
            //为空
            result.setCode("0");
            result.setMsg("密码为空");
            return result;
        }else {
            map.replace("password", MD5Utils.MD5Encode((String) map.get("password"), "utf-8", 10, false));
        }

        //username不为空时执行
        if(map.get("username")!=null&&!map.get("username").equals("")) {
            //查询用户名是否存在
            List<User> search= userMapper.selectUsername(map);
            //输出查询结果
            System.out.println("查询结果为:"+"\n"+search);
            //当查询出的结果为1时，设置值用户名已存在
            System.out.println("search.size():"+search.size());
            if (search.size() == 1) {
                result.setCode("0");
                result.setMsg("用户名已存在");
            } else {
                //将用户状态默认为1
                map.put("state","1");
                //当用户不存在且不为空时执行
                accountAndPermission.setTableName("accounts");
                accountAndPermission.setMap(map);
                int users = userMapper.insert(accountAndPermission);
                //注册成功
                //当返回值>0时，注册成功
                if (users == 1) {
                    //permission
                    AccountAndPermission permissionData = new AccountAndPermission();
                    permissionData.setTableName("accountpermissions");
                    //获取新账号自动生成的主键并传入准备的map中
                    permissionmap.put("account_id",accountAndPermission.getAccount_id());
                    //将值放入map中
                    permissionData.setMap(permissionmap);
                    //对账号权限表新增添加账号的权限
                    userMapper.insert(permissionData);
                    result.setCode("1");
                    result.setMsg("注册成功");
                } else {
                    result.setCode("0");
                    result.setMsg("注册失败");
                }
                System.out.println(users);
            }
        }else {
            //为空
            result.setCode("0");
            result.setMsg("账号为空");
        }

        return result;
    }
    //RemoveUser
    public Result RemoveUser(Map map) {
        Result result = new Result();
        if (!map.get("username").equals("")&& map.get("username")!=null){
            //删除用户
            int RemoveUserData = userMapper.RemoveUser(map);
            //删除用户信息
            int RemoveUserInfoData = userMapper.RemoveUserInfo(map);
            if (RemoveUserData == 1 &&RemoveUserInfoData == 1){
                result.setCode("1");
                result.setMsg("账号注销成功");
            }else {
                result.setCode("0");
                result.setMsg("账号注销失败");
            }
        }else {
            //为空
            result.setCode("0");
            result.setMsg("账号名为空");
        }
        return result;
    }
    @Override
    public Result SearchClientInfo(Map map) {
        Result result = new Result();
        if (!map.get("username").equals("")&& map.get("username")!=null){
            //搜索用户信息
            List<Map> data = userMapper.SearchClientInfo(map);
            if (data.size()==1){
                result.setCode("1");
                result.setData(data);
                result.setMsg("查询成功");
            }else {
                result.setCode("0");
                result.setMsg("未找到该用户信息");
            }
        }else {
            //为空
            result.setCode("0");
            result.setMsg("账号名为空");
        }
        return result;
    }

//    @Override
//    public Result adminSearch(Map map,HttpSession session) {
//        System.out.println("进入service，接收到的值为："+map);
//        Result result = new Result();
//        List<User> admin = userMapper.selectAdmin(map);
//
//        System.out.println("查询结果为:"+"\n"+admin);
//        //如果查询到有值则登录成功
//        if (admin.size()==1){
//            result.setCode("1");
//            result.setMsg("登陆成功");
//            result.setData(admin);
//            session.setAttribute("username",map.get("username"));
//        }else {
//            result.setCode("0");
//            result.setMsg("登陆失败");
//        }
//        return result;
//    }
}
